using System;
using System.Globalization;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using WebChart;

using FAQComponents;
using FAQComponents.API;
using FAQComponents.Objects;

namespace Adge.Web.Faq.Admin
{
	/// <summary>
	/// Summary description for Reports.
	/// </summary>
	public partial class Reports : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			PermissionAPI.EnsureAdministrator();

			if (!Page.IsPostBack)
			{
				// populate DDLs
				FAQ.DataSource = FAQAPI.GetAllFAQs(true);
				FAQ.DataBind();

				category.DataSource = CategoryAPI.GetAllCategories();
				category.DataBind();
				
				DateTimeFormatInfo dtfi = new DateTimeFormatInfo();				
				for (int i = 0; i < dtfi.MonthNames.Length; i++)
					if (dtfi.MonthNames[i].Trim().Length > 0)
						month.Items.Add(new ListItem(dtfi.MonthNames[i], (i + 1).ToString()));

				// select the current month
				month.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;

				// get years as 5 years past and 10 years future
				for (int i = DateTime.Now.Year - 5; i <= DateTime.Now.Year + 10; i++)
					year.Items.Add(new ListItem(i.ToString(), i.ToString()));

				// select the current year
				year.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
			}

		}		

		protected void btnShowReport_Click(object sender, System.EventArgs e)
		{
			reportChart.Visible = true;

            string virtPath = this.ResolveClientUrl("~/Files/charts/");//string.Concat(Request.ApplicationPath, "/faq/Admin/charts/").Replace("//", "/");
			WebChart.ChartControl.VirtualPath = virtPath;
			WebChart.ChartControl.PhysicalPath = Server.MapPath(virtPath);

			FAQViewCollection fvl = null;
			int m = InvariantString.ToInt32(month.SelectedItem.Value);
			int y = InvariantString.ToInt32(year.SelectedItem.Value);

			string dateName = new DateTime(y, m, 1).ToString("MMMM yyyy");

			if (radFAQ.Checked)
			{
				int FAQID = InvariantString.ToInt32(FAQ.SelectedItem.Value);
				reportChart.ChartTitle.Text = "Views for FAQ for " + dateName;
				fvl = FAQAPI.GetFAQViewsByFAQ(m, y, FAQID);
			}
			else if (radCategory.Checked)
			{
				reportChart.ChartTitle.Text = "Views for FAQs in Category " + CategoryAPI.GetCategory(InvariantString.ToInt32(category.SelectedItem.Value)).Name + " for " + dateName;
				fvl = FAQAPI.GetFAQViewsByCategory(m, y, InvariantString.ToInt32(category.SelectedItem.Value));
			}
			else 
			{
				reportChart.ChartTitle.Text = "Views for All FAQs for " + dateName;
				fvl = FAQAPI.GetFAQViews(m, y);
			}

			ColumnChart bc = new ColumnChart();
			bc.DataSource = fvl;
			bc.DataXValueField = "ViewDateDay";
			bc.DataYValueField = "Views";
			bc.Shadow.Visible = true;
			bc.Shadow.Color = Color.DarkGray;
			bc.DataBind();

			bc.Fill.Color = Color.BurlyWood;
			bc.Line.Color = Color.DarkKhaki;

			reportChart.Charts.Add(bc);
			reportChart.RedrawChart();
		}
	}
}
